<template>
  <div class="flex flex-col h-full">
    <searchContainer @search="queryList" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="mcbm" class="w-64">
          <a-input v-model:value="formState.mcbm" placeholder="员工编号/员工姓名/助记码" />
        </a-form-item>
        <a-form-item label="部门" name="bm">
          <SelectDepartment v-model:value="formState.bm" class="!w-64" dept-value="id" trigger="focus" multiple />
        </a-form-item>
        <a-form-item label="科室" name="ksList">
          <SelectSection v-model:value="formState.ksList" class="!w-64" trigger="focus" multiple />
        </a-form-item>
      </a-form>
    </searchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex flex-end">
        <BaseExport type="运维" name="员工维护" code="00001094" :search-params="formState">导出</BaseExport>
      </div>
      <baseTable
        :loading="loading"
        column-code="00001094"
        fill
        :data-source="tableData"
        bordered
        :scroll="{ x: 2150 }"
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-switch v-model:checked="record.status" checked-value="0" un-checked-value="1" @change="onConfirmState(record, $event)" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <confirmModal description="确定初始化?" @confirm="onInitialize(record)">
              <a-button type="link" size="small"> 初始化密码 </a-button>
            </confirmModal>
            <a-button type="link" size="small" @click="onSign(record)"> 电子签名 </a-button>
          </template>
        </template>
      </baseTable>
    </a-card>
    <SignModal v-model:open="openSign" :row-data="rowData" @success="queryList" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import searchContainer from '@/components/SearchContainer'
import baseTable from '@/components/BaseTable'
import confirmModal from '@/components/ConfirmModal'
import SignModal from './SignModal'

const loading = ref(false)
const tableData = ref([])
const formRef = ref(null)
const formState = ref({
  bm: [],
  ksList: []
})
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const onInitialize = async (record) => {
  const { code, msg } = await post('/operation/opex/doResetPassword', { ...record })
  if (code !== 0) return message.error(msg || '失败')
  message.success(msg)
  queryList()
}
const onConfirmState = async (row, number) => {
  const { code, msg } = await post('/operation/opex/doConfirmEmpInfo', { empId: row.empId, status: number === '0' ? '1' : '0' })
  if (code !== 0) return message.error(msg)
  message.success('操作成功')
  queryList()
}

const queryList = async () => {
  loading.value = true
  const { code, data, total } = await post('/operation/opex/getEmpInfoList', {
    ...formState.value,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  loading.value = false
  if (code !== 0) return
  tableData.value = data.map((item) => {
    item.status = item.status ? '1' : '0'
    return item
  })
  page.value.total = total
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
const openSign = ref(false)
const rowData = ref({})
const onSign = (row) => {
  rowData.value = { ...row }
  openSign.value = true
}
onMounted(() => {
  queryList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
